<?
ini_set('display_errors', 1);

include 'include.php';
$start = microtime_float();


rpc::$time = time();
log::debug('started', rpc::$time);

rpc::_prepare();
$check = rpc::_process_params();
if($check)
{
    rpc::_work();
    Statistic::add('requests_30', 1);
    Statistic::set_active_user();
}
else rpc::_add_response('error', 'params');
$response = rpc::_out();

$execution_time = microtime_float() - $start;
log::save(rpc::$user_id, rpc::$request, $execution_time, $response);
echo $response;
class rpc
{
    public static $params = null;
    public static $response = null;
    public static $request = null;
    public static $request_method = null;
    public static $user_id = null;
    public static $auth_key = null;

    public static $time = 0;

    public static function _prepare()
    {
        rpc::$response = new stdClass;
        rpc::$response->response = new stdClass;
    }

    public static function _process_params()
    {
        if($_GET && is_array($_GET))
        {
            foreach($_GET as $f => $v)
            {
                rpc::$params[$f] = $v;
            }
        }
        if($_POST && is_array($_POST))
        {
            foreach($_POST as $f => $v)
            {
                rpc::$params[$f] = $v;
            }
        }
        rpc::$request = json_encode(rpc::$params);
        rpc::$request_method = rpc::_get_value('method');
        rpc::$user_id = rpc::_get_value('user_id');
        rpc::$auth_key = rpc::_get_value('auth_key');
        if(!rpc::$request_method || !rpc::$user_id) return false;

        if(!rpc::validate(rpc::$user_id)) return false;

        return true;
    }

    public static function validate($uid)
    {
        $auth_key = rpc::_get_value('auth_key');
        $md5 = md5( _settings::$api_settings['app_id'].'_'.$uid.'_'. _settings::$api_settings['secret'] );
        $md5_2 = md5( _settings::$api_settings['app_id_2'].'_'.$uid.'_'. _settings::$api_settings['secret_2'] );
        if ($auth_key != $md5 && $auth_key != $md5_2) {
            rpc::_add_response('auth_key', $auth_key);
            return false;
        }
        return true;
    }

    public static function _get_value($name,$default=false)
    {
        if(isset(rpc::$params[$name]))
        {
            return rpc::$params[$name];
        }
        return $default;
    }

    public static function _work()
    {
        $method = rpc::$request_method;
        if(!$method) return false;
        switch($method)
        {
            // первый запрос
            case 'init':PlayerController::init();
                break;
            // посещение друга
            case 'visit_friend':PlayerController::visitFriend();
                break;
            // сажаем растение
            case 'plant':PlayerController::plant();
                break;
            // проставляем типы клеток админов
            case 'admin':PlayerController::admin();
                break;

        }
    }

    public static function _add_response($name,$value)
    {
        rpc::$response->response->$name = $value;
    }

    public static function _out()
    {
        rpc::$response->time = rpc::$time;
        return json_encode(rpc::$response);
    }
}
?>